package cn.com.guage.transtation.service;

import cn.com.guage.transtation.dao.OrderRepository;
import cn.com.guage.transtation.dao.StockRepository;
import cn.com.guage.transtation.entity.Order;
import cn.com.guage.transtation.entity.Stock;
import io.seata.saga.engine.StateMachineEngine;
import io.seata.saga.statelang.domain.StateMachineInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Service
public class OrderService {

    @Autowired
    private StateMachineEngine stateMachineEngine;

    @Autowired
    private OrderRepository orderRepository;

    @Autowired
    private StockRepository stockRepository;

    public void createOrder(Integer userId, Integer productId, Integer quantity) {
        // 定义业务参数
        Map<String, Object> params = new HashMap<>();
        params.put("userId", userId);
        params.put("productId", productId);
        params.put("quantity", quantity);

        // 启动状态机
        StateMachineInstance inst = stateMachineEngine.start("CreateOrderStateMachine", null, params);
        if ("FAILED".equals(inst.getStatus())) {
            throw new RuntimeException("订单创建失败");
        }
    }

    @Transactional
    public void createOrderAction(Map<String, Object> params) {
        Integer userId = (Integer) params.get("userId");
        Integer productId = (Integer) params.get("productId");
        Integer quantity = (Integer) params.get("quantity");

        Order order = new Order();
        order.setUserId(userId);
        order.setProductId(productId);
        order.setQuantity(quantity);
        order.setStatus("CREATED");
        orderRepository.save(order);
    }

    @Transactional
    public void cancelOrderAction(Map<String, Object> params) {
        // 这里简单示例，实际可能需要根据业务逻辑删除订单
        System.out.println("取消订单：" + params);
    }

    @Transactional
    public void deductStockAction(Map<String, Object> params) {
        Integer productId = (Integer) params.get("productId");
        Integer quantity = (Integer) params.get("quantity");

        Optional<Stock> stockOptional = stockRepository.findByProductId(productId);
        if (stockOptional.isPresent()) {
            Stock stock = stockOptional.get();
            if (stock.getQuantity() >= quantity) {
                stock.setQuantity(stock.getQuantity() - quantity);
                stockRepository.save(stock);
            } else {
                throw new RuntimeException("库存不足");
            }
        } else {
            throw new RuntimeException("未找到对应商品库存");
        }
    }

    @Transactional
    public void restoreStockAction(Map<String, Object> params) {
        Integer productId = (Integer) params.get("productId");
        Integer quantity = (Integer) params.get("quantity");

        Optional<Stock> stockOptional = stockRepository.findByProductId(productId);
        if (stockOptional.isPresent()) {
            Stock stock = stockOptional.get();
            stock.setQuantity(stock.getQuantity() + quantity);
            stockRepository.save(stock);
        }
    }
}    